package com.njmd.mdbs.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.njmd.framework.base.BaseCrudServiceImpl;
import com.njmd.framework.dao.BaseHibernateDAO;
import com.njmd.framework.dao.PropertyFilter;
import com.njmd.mdbs.web.constants.ResultConstants;
import com.njmd.mdbs.web.dao.SysPermissionDAO;
import com.njmd.mdbs.web.entity.sys.SysPermission;
import com.njmd.mdbs.web.service.SysPermissionService;

/**
 * 权限管理
 * 
 * @author sunqw
 * @since 2013.10.12
 * 
 */
@Service
@Transactional(readOnly = true)
public class SysPermissionServiceImpl extends BaseCrudServiceImpl<SysPermission, Long> implements SysPermissionService
{
	/**
	 * 权限信息数据访问对象
	 */
	@Autowired
	private SysPermissionDAO sysPermissionDAO;

	/**
	 * 根据系统类型查询菜单信息
	 * 
	 * @param systemId
	 *            　系统类型
	 * @return　权限信息集合
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SysPermission> findMenuBySystemId(Integer systemId) throws Exception
	{
		String hql = "from SysPermission as model where model.systemId =? and model.status=1 and model.permissionType=1 order by model.permissionSort asc";
		return sysPermissionDAO.findByHql(hql, systemId);
	}

	/**
	 * 根据系统类型查询菜单信息
	 * 
	 * @param systemId
	 *            　系统类型
	 * @return　权限信息集合
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SysPermission> findLevel1Menu(Integer systemId) throws Exception
	{
		String hql = "from SysPermission as model where model.systemId =? and model.status = 1 and model.permissionType=1 and (model.parentPermissionId is null or model.parentPermissionId=0) order by model.permissionSort asc";
		return sysPermissionDAO.findByHql(hql, systemId);
	}

	/**
	 * 通过父id和权限类型找到相对应的权限list
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SysPermission> findMenuByparentIdAndType(Long parentId, Long permissionType) throws Exception
	{
		List<SysPermission> permissionList = new ArrayList<SysPermission>();
		List<SysPermission> resultList = new ArrayList<SysPermission>();
		if (null != permissionType)
		{
			if (parentId == -1)// 属于运营商系统
			{
				String hql = "from SysPermission as model where model.systemId =? and model.status=1 and model.permissionType=? and model.parentPermissionId is null order by model.permissionSort asc";
				Object[] values = new Object[] { 1L, permissionType };
				permissionList = sysPermissionDAO.findByHql(hql, values);
			}
			else if (parentId == -2)// 属于会员系统
			{
				String hql = "from SysPermission as model where model.systemId =? and model.status=1 and model.permissionType=? and model.parentPermissionId is null order by model.permissionSort asc";
				Object[] values = new Object[] { 2L, permissionType };
				permissionList = sysPermissionDAO.findByHql(hql, values);
			}
			else
			{
				List<PropertyFilter> filterList = new ArrayList<PropertyFilter>();
				filterList.add(new PropertyFilter("parentPermissionId", PropertyFilter.MatchType.EQ, parentId));
				filterList.add(new PropertyFilter("permissionType", PropertyFilter.MatchType.EQ, permissionType));
				filterList.add(new PropertyFilter("status", PropertyFilter.MatchType.EQ, "1"));

				permissionList = sysPermissionDAO.findByFilters(filterList, Order.asc("permissionSort"));
			}
		}
		else
		{
			if (parentId == -1)// 属于运营商系统
			{
				String hql = "from SysPermission as model where model.systemId =? and model.status=1 and model.parentPermissionId is null order by model.permissionSort asc";
				Object[] values = new Object[] { 1L };
				permissionList = sysPermissionDAO.findByHql(hql, values);
			}
			else if (parentId == -2)// 属于会员系统
			{
				String hql = "from SysPermission as model where model.systemId =? and model.status=1 and model.parentPermissionId is null order by model.permissionSort asc";
				Object[] values = new Object[] { 2L };
				permissionList = sysPermissionDAO.findByHql(hql, values);
			}
			else
			{
				List<PropertyFilter> filterList = new ArrayList<PropertyFilter>();
				filterList.add(new PropertyFilter("parentPermissionId", PropertyFilter.MatchType.EQ, parentId));
				filterList.add(new PropertyFilter("status", PropertyFilter.MatchType.EQ, "1"));

				permissionList = sysPermissionDAO.findByFilters(filterList, Order.asc("permissionSort"));
			}
		}
		for (SysPermission frameSysMenu : permissionList)
		{
			SysPermission frameSysMenuDTO = new SysPermission();
			BeanUtils.copyProperties(frameSysMenuDTO, frameSysMenu);
			resultList.add(frameSysMenuDTO);
		}
		return resultList;
	}

	/**
	 * 通过权限id删除权限
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Throwable.class)
	public int delete(Long menuId) throws Exception
	{
		SysPermission entity = baseDao.findById(menuId);
		// 获取 需要删除菜单的treeCode 然后 将属于这个treeCode的下级菜单 全部删除
		String hql = "from SysPermission as t where t.treeCode like '" + entity.getTreeCode() + "%' order by t.permissionSort asc";
		@SuppressWarnings("unchecked")
		List<SysPermission> list = sysPermissionDAO.findByHql(hql);
		/**
		 * LiuFeng 2014-7-8 获取比要删除菜单sort大的 获取后 判断删除的是一级菜单还是二级菜单或者三级菜单 根据 当前的菜单等级
		 * 一级：就将排在此菜单后面的所有菜单的Sort统统减去100 二级：就将在这个同上级菜单下得到比自己大的所有菜单减去10 三级：同二级 减去
		 * 1
		 */
		String hql2 = "from SysPermission as t where t.permissionSort > ? order by t.permissionSort asc";
		@SuppressWarnings("unchecked")
		List<SysPermission> list2 = sysPermissionDAO.findByHql(hql2, entity.getPermissionSort());
		// 删除该菜单为 一级菜单 循环得到所有下面排序sort大的 修改 减去100 就算同时修改了他的子菜单
		// 也不会影响下面的删除
		if (list2.size() > 0)
		{
			if (entity.getSysPermissionTreeSize() == 1)
			{
				for (int i = 0; i < list2.size(); i++)
				{
					int newSort = list2.get(i).getPermissionSort() - 100;
					list2.get(i).setPermissionSort(newSort);
				}
			}
			else if (entity.getSysPermissionTreeSize() == 2)
			{
				for (int i = 0; i < list2.size(); i++)
				{
					if (entity.getParentPermissionId().equals(list2.get(i).getParentPermissionId()))
					{
						int newSort = list2.get(i).getPermissionSort() - 10;
						list2.get(i).setPermissionSort(newSort);
					}
				}
			}
			else if (entity.getSysPermissionTreeSize() == 3)
			{
				for (int i = 0; i < list2.size(); i++)
				{
					if (entity.getParentPermissionId().equals(list2.get(i).getParentPermissionId()))
					{
						int newSort = list2.get(i).getPermissionSort() - 1;
						list2.get(i).setPermissionSort(newSort);
					}
				}
			}
			baseDao.update(list2);
		}
		if (list.size() != 0)
		{
			for (int i = 0; i < list.size(); i++)// 循环删除子菜单
			{
				sysPermissionDAO.deleteById(list.get(i).getPermissionId());
			}
		}
		return ResultConstants.DELETE_SUCCEED;
	}

	/**
	 * 添加菜单
	 * 
	 * 2014-6-30 Liufeng
	 * 
	 * @param sysPermissionupd
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, rollbackFor = Throwable.class)
	public int save(SysPermission sysPermission) throws Exception
	{
		Long parentId = sysPermission.getParentPermissionId();
		String hql = "from SysPermission as t where t.permissionId=? or t.parentPermissionId=? and t.systemId =? order by t.permissionSort desc";
		Object[] values = new Object[] { parentId, parentId, 1 };
		List<SysPermission> seeList = sysPermissionDAO.findByHql(hql, values);
		// 取出平级TreeCode 根据分割个数 判断 sort=+100、+10、+1
		String flatTreeCode = seeList.get(0).getTreeCode();
		// 根据分割的treeCode 分割数量来判断添加的是几级菜单 例：,1, 截取到两个 以此类推
		int codeSize = flatTreeCode.split(",").length - 1;
		int sort = seeList.get(0).getPermissionSort();
		if (codeSize == 1)
			if (parentId.equals(seeList.get(0).getParentPermissionId()))
				sort += 100;
			else
				sort += 10;
		else if (codeSize == 2)
			if (parentId.equals(seeList.get(0).getParentPermissionId()))// 判断本次数据与查出来的seeList里面的上级Id是否相同
																		// 如果不同则是
																		// 在查出来的数据下级加
				sort += 10;
			else
				sort += 1;
		else if (codeSize == 3)
			sort += 1;

		sysPermission.setSystemId(1);
		sysPermission.setPermissionSort(sort);
		sysPermissionDAO.save(sysPermission);
		String treeCode = String.valueOf("," + sysPermission.getPermissionId() + ",");
		String parentTreeCode = "";
		if (sysPermission.getParentPermissionId() != 0)
		{
			parentTreeCode = baseDao.findById(sysPermission.getParentPermissionId()).getTreeCode();
			treeCode = parentTreeCode + sysPermission.getPermissionId() + ",";
		}
		sysPermission.setTreeCode(treeCode);
		baseDao.update(sysPermission);
		return ResultConstants.SAVE_SUCCEED;
	}

	/**
	 * 编辑保存菜单管理
	 * 
	 * @param sysPermissionupd
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "unused" })
	@Override
	@Transactional(readOnly = false, rollbackFor = Throwable.class)
	public int update(SysPermission sysPermission) throws Exception
	{
		Long pId = sysPermission.getParentPermissionId();// 新保存进的上级ID
		List<SysPermission> seeList = null;

		Long sysId = sysPermission.getPermissionId();// 本次数据ID
		// 首先判断本次有没有修改上级的Id
		String[] codeSize = sysPermission.getTreeCode().split(",");// 分割该条数据的TreeCode
																	// 便于取出原上级ID
		Long oneId = null;
		String deletetree = "";
		// 判断 保存修改中的上级ID与treecode中的上级ID是否相同 如果不同 就取出此条数据的原上级ID
		if (codeSize.length == 2)
		{// 一级菜单
			if (!pId.equals(Long.valueOf(0)))
			{
				oneId = Long.valueOf(0);
				deletetree = "0";
			}
		}
		else if (codeSize.length == 3)
		{// 二级菜单
			if (!pId.equals(Long.valueOf(codeSize[1])))
			{
				oneId = Long.valueOf(codeSize[1]);
				deletetree = codeSize[1];
			}
		}
		else if (codeSize.length == 4)
		{// 三级菜单
			if (!pId.equals(Long.valueOf(codeSize[2])))
			{
				oneId = Long.valueOf(codeSize[2]);
				deletetree = codeSize[2];
			}
		}
		if (!deletetree.equals(""))
		{// 判断上级ID是否移动 移动过oneId则不为空 找到自己的id
			String hql = "from SysPermission as t where t.permissionId=? or t.parentPermissionId=? and t.systemId =? order by t.permissionSort desc";
			Object[] values = new Object[] { sysPermission.getParentPermissionId(), sysPermission.getParentPermissionId(), 1 };// 查找自己的ID与自己的子目录
			seeList = sysPermissionDAO.findByHql(hql, values);
			// 当进入此判断 说明 deletetree已经获得原上级ID 两种可能 1：从子菜单变成 一级菜单
			// 2：从这个子菜单变成另外一个子菜单 根据 上级Id来判断 当等于0 就是变为一级菜单
			// 取出平级TreeCode 根据分割个数 判断 sort=+100、+10、+1
			String flatTreeCode = seeList.get(0).getTreeCode();// 将要转如ID
			// 根据分割的treeCode 分割数量来判断添加的是几级菜单 例：,1, 截取到两个 以此类推
			int codelat = flatTreeCode.split(",").length - 1;
			int sort = seeList.get(0).getPermissionSort();

			if (codelat == 1)
				if (pId.equals(seeList.get(0).getParentPermissionId()))
					sort += 100;
				else
					sort += 10;
			else if (codelat == 2)
				if (pId.equals(seeList.get(0).getParentPermissionId()))// 判断本次数据与查出来的seeList里面的上级Id是否相同
																		// 如果不同则是
																		// 在查出来的数据下级加
					sort += 10;
				else
					sort += 1;
			else if (codelat == 3)
				sort += 1;
			sysPermission.setPermissionSort(sort);// 本次修改的
			sysPermission.setParentPermissionId(pId);
			// 自己ID的tree长度
			// int
			// LongId=oldList.get(0).getPermissionId().toString().length()+1;//本条数据
			// id+一个逗号
			// int LongTree = oldList.get(0).getTreeCode().substring(0,
			// sysP.getTreeCode().length()-LongId).length();//获取上级tree长度
			String newtree = seeList.get(seeList.size() - 1).getTreeCode();// 新的上级tree
			if (pId != 0)
			{
				sysPermission.setTreeCode(newtree + sysPermission.getPermissionId() + ",");// 新的tree
																							// +
																							// 本条tree-上级长度
			}
			else
			{
				sysPermission.setTreeCode("," + sysPermission.getPermissionId() + ",");
			}
			sysPermissionDAO.update(sysPermission);
			// 先取得本次数据下面所有的下级菜单数据
			hql = "from SysPermission as t where  t.parentPermissionId = ?  and t.systemId =? order by t.permissionSort asc";
			List<SysPermission> oldList = sysPermissionDAO.findByHql(hql, sysPermission.getPermissionId(), 1);// 查找自己的ID与自己的子目录
			int newsort = 1;
			if (oldList.size() > 0)
			{// 判断是否还有子级菜单
				for (int i = 0; i < oldList.size(); i++)
				{// 循环给子级菜单叠加sort
					int s = sort - oldList.get(i).getPermissionSort();
					if (codelat == 1)
					{
						if (pId.equals(seeList.get(0).getParentPermissionId()))// 判断本次数据与查出来的seeList里面的上级Id是否相同
																				// 如果不同则是
																				// 在查出来的数据下级加
							oldList.get(i).setPermissionSort(sort + ((i + newsort) * 10));
						else
							oldList.get(i).setPermissionSort(sort + (i + newsort));
					}
					else
					{
						oldList.get(i).setPermissionSort(sort + (i + newsort));
					}
					if (pId != 0)
					{
						oldList.get(i).setTreeCode(newtree + sysPermission.getPermissionId() + "," + oldList.get(i).getPermissionId() + ",");// 新的tree
																																				// +
																																				// 本条tree-上级长度
					}
					else
					{
						oldList.get(i).setTreeCode("," + sysPermission.getPermissionId() + "," + oldList.get(i).getPermissionId() + ",");
					}
				}
				baseDao.update(oldList);
			}
		}
		else
		{
			sysPermissionDAO.update(sysPermission);
			return ResultConstants.UPDATE_SUCCEED;
		}
		return ResultConstants.UPDATE_SUCCEED;
	}

	/**
	 * 改变菜单的排序
	 * 
	 * @param menuId
	 * @param sortOffset
	 *            1－下移 -1－上移
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, rollbackFor = Throwable.class)
	public void changSort(Long menuId, int sortOffset) throws Exception
	{
		SysPermission frameSysMenu = sysPermissionDAO.findById(menuId);
		Long parentId = frameSysMenu.getParentPermissionId();
		Integer sort = frameSysMenu.getPermissionSort();
		Integer newSort = sort + sortOffset;
		String hql = null;
		Object[] values = null;
		if (parentId == null)
		{
			hql = "from SysPermission as model where model.systemId =? and model.permissionSort =? and model.parentPermissionId is null";
			values = new Object[] { frameSysMenu.getSystemId(), newSort };
		}
		else
		{
			hql = "from SysPermission as model where model.systemId =? and model.permissionSort =? and model.parentPermissionId =?";
			values = new Object[] { frameSysMenu.getSystemId(), newSort, frameSysMenu.getParentPermissionId() };
		}
		List<SysPermission> menuList = sysPermissionDAO.findByHql(hql, values);
		if (menuList != null && menuList.size() > 0)
		{
			SysPermission menu = menuList.get(0);
			menu.setPermissionSort(sort);
			frameSysMenu.setPermissionSort(newSort);
			sysPermissionDAO.update(menu);
			sysPermissionDAO.update(frameSysMenu);
		}
	}

	@Autowired
	@Qualifier(value = "sysPermissionDAO")
	@Override
	public void setBaseDao(BaseHibernateDAO<SysPermission, Long> baseDao)
	{
		this.baseDao = baseDao;
	}

	@Override
	public List<SysPermission> sortList(List<SysPermission> list) throws Exception
	{
		if (list.isEmpty())
		{
			return new ArrayList<SysPermission>();
		}
		else
		{
			int size = list.size();
			Long[] pIds = new Long[size];
			for (int i = 0; i < size; i++)
			{
				pIds[i] = list.get(i).getPermissionId();
			}
			String hql = "from SysPermission as model where model.permissionId in (:pIds) order by model.permissionSort asc";
			Map m = new HashMap();
			m.put("pIds", pIds);
			List<SysPermission> spList = baseDao.findByHql(hql, m);
			return spList;
		}
	}


	@Override
	public List<SysPermission> findAllNoId(Long id) throws Exception
	{
		if (id == 0)
		{// 新增的菜单 暂无TreeCode 全部显示 除三级菜单、通过实体getSysPermissionTreeSize在页面进行判断
			String hql = "from SysPermission as model order by model.permissionSort asc";
			@SuppressWarnings("unchecked")
			List<SysPermission> spList = baseDao.findByHql(hql);
			return spList;
		}
		else
		{
			SysPermission entity = baseDao.findById(id);
			// 控制编辑页面下拉框显示问题
			String hql = "";
			hql = "from SysPermission as model where model.permissionId != ? and model.treeCode not like '" + entity.getTreeCode()
					+ "%' order by model.permissionSort asc";
			@SuppressWarnings("unchecked")
			List<SysPermission> spList = baseDao.findByHql(hql, id);
			// 获取该移动的菜单 共有几个等级的菜单
			hql = "from SysPermission as model where model.permissionId = ? or model.treeCode like '" + entity.getTreeCode()
					+ "%' order by model.permissionSort asc";
			@SuppressWarnings("unchecked")
			List<SysPermission> oneSysList = baseDao.findByHql(hql, id);
			int treeSize = 0;
			int treeLength = 0;
			int treeSize2 = entity.getSysPermissionTreeSize();// 当前移动菜单的TreeCode长度
																// 得到
																// 移动的ID是属于一级、二级、三级
			for (int i = 0; i < oneSysList.size(); i++)
			{
				treeLength = oneSysList.get(i).getSysPermissionTreeSize();// 循环遍历
																			// 取出
																			// 当前菜单下所有的treecode
																			// 主要得到此菜单下菜单等级有几个
																			// 1
																			// 2
																			// 3
				if (treeSize < treeLength)
				{// 获取到最长的一个下级菜单
					treeSize = treeLength;
				}
			}
			int size = treeSize - treeSize2;
			int niceTree = 0;
			if (size == 0)
			{// 除了自己没有下级菜单，显示一级与二级下拉菜单
				niceTree = 3;
			}
			else if (size == 1)
			{// 有一个下级菜单，只显示一级菜单进行选择。 选择的一级菜单加上本身的二级菜单 即可最大形成三级菜单
				niceTree = 2;
			}
			else if (size == 2)
			{// 有两个下级菜单需要一起移动 这时候就不需要显示其他的菜单！ 本身就是一个三级菜单！
				niceTree = 1;
			}
			@SuppressWarnings({ "unchecked", "rawtypes" })
			List<SysPermission> newspList = new ArrayList();
			for (int i = 0; i < spList.size(); i++)
			{// 循环将三级菜单排除 三级菜单不会出现在 上级下拉选择内
				if (spList.get(i).getSysPermissionTreeSize() < niceTree)
				{// TreeCode长度小于niceTree个的菜单 进行显示
					newspList.add(spList.get(i));
				}
			}
			return newspList;
		}
	}

	/**
	 * 上移下移菜单
	 * 
	 * @param sysPermissionupd
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, rollbackFor = Throwable.class)
	public int upORdown(Long id, int upORdown) throws Exception
	{
		SysPermission sysA = sysPermissionDAO.findById(id);// 获取本次主移动的菜单数据
		String hql = "";
		// 找出需要移动菜单的平级的所有菜单 方便 进行 上下移动 获取集合
		hql = "from SysPermission as model where model.systemId =? and model.parentPermissionId =? order by model.permissionSort asc";
		@SuppressWarnings("unchecked")
		List<SysPermission> sysBList = baseDao.findByHql(hql, sysA.getSystemId(), sysA.getParentPermissionId());
		int size = 0;
		// 计算出需要上移或下移的数据为当前排序的第几位
		for (int i = 0; i < sysBList.size(); i++)
		{
			if (sysBList.get(i).getPermissionId().equals(id))
			{
				if (upORdown == 1)
				{// upORdown==1就是需要下移 那么size=I+1取得下面那条数据
					size = i + 1;
				}
				else
				{
					size = i - 1;
				}
			}
		}
		SysPermission sysB = sysBList.get(size);// 将被移动的菜单数据放入实体内、
		String flatTreeCode = sysA.getTreeCode();
		// 根据分割的treeCode 分割数量来判断添加的是几级菜单 例：,1, 截取到两个 以此类推
		int codeSize = flatTreeCode.split(",").length - 1;
		int sysAsort = sysA.getPermissionSort();// 获取主移动菜单的sort
		int sort = 0;
		if (codeSize == 1)
		{
			sort += 100;
		}
		else if (codeSize == 2)
		{
			sort += 10;
		}
		else if (codeSize == 3)
		{
			sort += 1;
		}
		String treeCodeA = sysA.getTreeCode();
		int treeCodeALength = treeCodeA.length() - 1;
		String treeCodeB = sysB.getTreeCode();
		int treeCodeBLength = treeCodeB.length() - 1;
		// 找出主移动的所有子菜单及自己
		hql = "from SysPermission as model where model.systemId =? and model.treeCode like '" + treeCodeA + "%'  order by model.permissionSort asc";
		@SuppressWarnings("unchecked")
		List<SysPermission> AList = baseDao.findByHql(hql, sysA.getSystemId());
		// 找出被移动的所有子菜单及自己
		hql = "from SysPermission as model where model.systemId =? and model.treeCode like '" + treeCodeB + "%'  order by model.permissionSort asc";
		@SuppressWarnings("unchecked")
		List<SysPermission> BList = baseDao.findByHql(hql, sysB.getSystemId());
		for (int i = 0; i < AList.size(); i++)
		{
			int sortA = AList.get(i).getPermissionSort();
			if (upORdown == 1)
			{
				AList.get(i).setPermissionSort(sortA + sort);
			}
			else
			{
				AList.get(i).setPermissionSort(sortA - sort);
			}
			// String treeA= AList.get(i).getTreeCode();
			// AList.get(i).setTreeCode(treeCodeB.substring(0,treeCodeALength)+treeA.substring(treeCodeALength));
		}
		for (int i = 0; i < BList.size(); i++)
		{
			int sortB = BList.get(i).getPermissionSort();
			if (upORdown == 1)
			{
				BList.get(i).setPermissionSort(sortB - sort);
			}
			else
			{
				BList.get(i).setPermissionSort(sortB + sort);
			}
			// String treeB= BList.get(i).getTreeCode();
			// BList.get(i).setTreeCode(treeCodeA.substring(0,treeCodeBLength)+treeB.substring(treeCodeBLength));
		}
		baseDao.update(AList);
		baseDao.update(BList);
		return ResultConstants.UPDATE_SUCCEED;
	}

	/**
	 * 取出所有的上级ID并去除重复
	 * 
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List<String> prantId()
	{
		// 查找出所有的菜单
		String hql = "from SysPermission as model order by model.parentPermissionId asc";// 查询出所有的上级Id
		List<SysPermission> spList1 = baseDao.findByHql(hql);
		// 得到所有的上级ID 将parntId放入数组中
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < spList1.size(); i++)
		{
			list.add(spList1.get(i).getParentPermissionId().toString());
		}
		// 将所有的上级ID 去除重复
		for (int i = 0; i < list.size() - 1; i++)
		{
			for (int j = list.size() - 1; j > i; j--)
			{
				if (list.get(j).equals(list.get(i)))
				{
					list.remove(j);
				}
			}
		}
		HashSet<String> h = new HashSet<String>(list);
		list.clear();
		list.addAll(h);
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysPermission> findUp()
	{
		@SuppressWarnings({ "rawtypes" })
		List<SysPermission> upList = new ArrayList();
		List list = prantId();// 得到不重复的ID 循环遍历 得到 属于该菜单所有的下级菜单除了最上面的一个
		System.out.println(list);
		// 循环取得list中所对应的下级菜单
		for (int i = 0; i < list.size(); i++)
		{// 循环得出不需要向上移动的
			String hql = "from SysPermission as model where model.parentPermissionId='" + list.get(i) + "' order by model.permissionSort asc";
			List<SysPermission> spList = baseDao.findByHql(hql);
			if (spList != null)
			{
				for (int j = 1; j < spList.size(); j++)
				{
					upList.add(spList.get(j));
				}
			}
		}
		return upList;
	}

	@Override
	public List<SysPermission> findDown()
	{
		@SuppressWarnings({ "rawtypes" })
		List<SysPermission> downList = new ArrayList();
		List list = prantId();// 得到不重复的ID 循环遍历 得到 属于该菜单所有的下级菜单最上面的一个
		System.out.println(list);
		// 循环取得list中所对应的下级菜单
		for (int i = 0; i < list.size(); i++)
		{// 循环得出不需要向下移动的
			String hql = "from SysPermission as model where model.parentPermissionId='" + list.get(i) + "' order by model.permissionSort asc";
			List<SysPermission> spList = baseDao.findByHql(hql);
			if (spList != null)
			{
				for (int j = 0; j < spList.size() - 1; j++)
				{
					downList.add(spList.get(j));
				}
			}
		}
		return downList;
	}
	
	@Override
	public List<SysPermission> findAll() throws Exception {
		return baseDao.findAll(Order.asc("permissionSort"));
	}

}
